package jjn.acwing.algorithm_basic.chapter2.stack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Jiang Jining
 * @since 2023-09-28 23:05
 */
public class Acwing828 {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bufferedReader.readLine());
        int index = 0;
        int[] stack = new int[100010];
        while (n-- > 0) {
            String command = bufferedReader.readLine();
            String[] parts = command.split(" ");
            String first = parts[0];
            if ("push".equals(first)) {
                stack[index++] = Integer.parseInt(parts[1]);
                continue;
            }
            if ("pop".equals(first)) {
                index--;
                continue;
            }
            if ("empty".equals(first)) {
                System.out.println(index == 0 ? "YES" : "NO");
                continue;
            }
            if ("query".equals(first)) {
                System.out.println(stack[index - 1]);
            }
        }
    }
}
